package com.youlin;

import org.quartz.*;

import java.time.LocalTime;

@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class HelloQuartzJob implements Job {

    Object k; // 自动注入 mergedJobDataMap 对应 k-v 键值对
    Object jk0;

    @Override
    public void execute(JobExecutionContext context) {
        JobDetail jobDetail = context.getJobDetail();
        JobKey key = jobDetail.getKey();
        System.out.printf("\033[32m %s ===>> %s ===>> %s ===>> %s \033[0m\n",
                LocalTime.now(), key.getName(), key.getGroup(), jk0);
//        System.out.printf("\033[32m %s ===>> %s ===>> %s ===>> %s \033[0m\n",
//                LocalTime.now(), key.getName(), key.getGroup(), jobDetail.getJobDataMap().get("jk0"));
    }

    public void execute2(JobExecutionContext context) {
        JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
        Integer jv0 = (Integer) mergedJobDataMap.get("jk0");
        Object jv1 = mergedJobDataMap.get("jk1");
        Object jv2 = mergedJobDataMap.get("jk2");
        Integer tv0 = (Integer) mergedJobDataMap.get("tk0");
        Object tv1 = mergedJobDataMap.get("tk1");
        Object tv2 = mergedJobDataMap.get("tk2");
//        Object v = mergedJobDataMap.get("k");
        Integer sk0 = null;
        Object skey;
        try {
            sk0 = (Integer) context.getScheduler().getContext().get("sk0");
            skey = context.getScheduler().getContext().get("skey");
            System.out.println(sk0+" : "+skey+" : "+context.getScheduler().getContext().hashCode());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        System.out.println(jv0+" : "+jv1+" : "+jv2+" : "+mergedJobDataMap.hashCode());
        System.out.println(tv0+" : "+tv1+" : "+tv2+" : "+mergedJobDataMap.hashCode());
        System.out.println(k+" : "+"hello ===>> "+ LocalTime.now());
        mergedJobDataMap.put("jk0", jv0+1);
        mergedJobDataMap.put("tk0", tv0+1);
        try {
            context.getScheduler().getContext().put("sk0", sk0+1);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public void execute1(JobExecutionContext context) {
        Integer jv0 = (Integer) context.getJobDetail().getJobDataMap().get("jk0");
        Object jv1 = context.getJobDetail().getJobDataMap().get("jk1");
        Object jv2 = context.getJobDetail().getJobDataMap().get("jk2");
        Integer tv0 = (Integer) context.getTrigger().getJobDataMap().get("tk0");
        Object tv1 = context.getTrigger().getJobDataMap().get("tk1");
        Object tv2 = context.getTrigger().getJobDataMap().get("tk2");
        Integer sk0 = null;
        Object skey;
        try {
            sk0 = (Integer) context.getScheduler().getContext().get("sk0");
            skey = context.getScheduler().getContext().get("skey");
            System.out.println(sk0+" : "+skey+" : "+context.getScheduler().getContext().hashCode());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        System.out.println(jv0+" : "+jv1+" : "+jv2+" : "+context.getJobDetail().getJobDataMap().hashCode());
        System.out.println(tv0+" : "+tv1+" : "+tv2+" : "+context.getTrigger().getJobDataMap().hashCode());
        System.out.println("hello ===>> "+ LocalTime.now());
        context.getJobDetail().getJobDataMap().put("jk0", jv0+1);
        context.getTrigger().getJobDataMap().put("tk0", tv0+1);
        try {
            context.getScheduler().getContext().put("sk0", sk0+1);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public Object getK() {
        return k;
    }

    public void setK(Object k) {
        this.k = k;
    }

    public Object getJk0() {
        return jk0;
    }

    public void setJk0(Object jk0) {
        this.jk0 = jk0;
    }
}
